package com.dualsportmaps.android.views;

import java.util.List;

import com.dualsportmaps.android.DsmResourceManager;
import com.dualsportmaps.android.SimpleTrackpoint;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.OsmandMapTileView;

public class DsmActiveLogLayer implements OsmandMapLayer {
	private OsmandMapTileView view;
	private Path path;
	private SavingTrackHelper savingTrackHelper;
	private void initUI() {
		path = new Path();
		savingTrackHelper = new SavingTrackHelper(DsmResourceManager.getApplicationContext());
	}
	@Override
	public void initLayer(OsmandMapTileView view) {
		this.view = view;
		initUI();
	}

	@Override
	public void onDraw(Canvas canvas, RectF latLonBounds, boolean nightMode) {
		List<SimpleTrackpoint> l;
		try {
			l = savingTrackHelper.getActiveLog();
		} catch (Exception e) {
			
			e.printStackTrace();
			return;
		}
		if(l.size() > 1) {
			canvas.save();
			if(view.showPerspective()) 
				view.addPerspective(canvas);
			if(view.getRotate() != 0)
				canvas.rotate(view.getRotate(), view.getCenterPointX(), view.getCenterPointY());
			Paint paint = getTrackPaint("#ff8000");
			path.rewind();
			int startIndex = -1;							
			for (int i = 0; i < l.size(); i++) {
				SimpleTrackpoint ls = l.get(i);
				if (startIndex == -1) {
					if (ls.getLatitude() >= latLonBounds.bottom && ls.getLatitude() <= latLonBounds.top  && ls.getLongitude() >= latLonBounds.left 
							&& ls.getLongitude() <= latLonBounds.right ) {
						startIndex = i > 0 ? i - 1 : i;
					}
				} else if (!(latLonBounds.left <= ls.getLongitude() + 0.03 && ls.getLongitude() - 0.03 <= latLonBounds.right
						&& latLonBounds.bottom <= ls.getLatitude() + 0.03 && ls.getLatitude() - 0.03 <= latLonBounds.top)) {
					drawSegment(canvas, l, startIndex, i, paint);
					startIndex = -1;
				}
			}
			if (startIndex != -1) {
				drawSegment(canvas, l, startIndex, l.size() - 1, paint);
			}
			
			canvas.restore();
		}
	}
	
	private void drawSegment(Canvas canvas, List<SimpleTrackpoint> l, int startIndex, int endIndex, Paint paint) {
		double startLng = l.get(startIndex).getLongitude();
		double startLat = l.get(startIndex).getLatitude();
		int px = view.getMapXForPoint(startLng);
		int py = view.getMapYForPoint(startLat);
		path.moveTo(px,py);
		for (int i = startIndex + 1; i <= endIndex; i++) {
			SimpleTrackpoint p = l.get(i);
			int x = view.getMapXForPoint(p.getLongitude());
			int y = view.getMapYForPoint(p.getLatitude());
			path.lineTo(x,y);
		}
		canvas.drawPath(path, paint);
	}
	
	@Override
	public void destroyLayer() {
		view = null;
	}

	@Override
	public boolean onTouchEvent(PointF point) {
		return false;
	}

	@Override
	public boolean onLongPressEvent(PointF point) {
		return false;
	}

	@Override
	public boolean drawInScreenPixels() {
		return true;
	}

	private Paint getTrackPaint(String color) {
		int width = 2;
		int c = Color.parseColor(color);
		if(view.getZoom() > 16)
			width = 5;
		else if(view.getZoom() > 14)
			width = 4;
		else if(view.getZoom() > 11)
			width = 3;
		else if(view.getZoom() > 8)
			width = 2;
		else
			width = 2;
		Paint paint = new Paint();
		paint.setColor(c);	
		paint.setAlpha(230);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(width);
		paint.setStrokeCap(Cap.ROUND);
		paint.setStrokeJoin(Join.ROUND);
		return paint;
	}
	
}
